package com.system.function.service;

import com.system.Tools.TimeTools;
import com.system.function.dao.WarehousingDAO;
import com.system.function.object.Commodity;
import com.system.function.object.Warehousing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WarehousingService {
    @Autowired
    private WarehousingDAO WarehousingDAO;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private TimeTools timeTools;

    //查询全部
    public List<Warehousing> getAllWarehousing(Warehousing Warehousing){
        List<Warehousing> allWarehousing = new ArrayList<>();
        try{
            List<Warehousing> allWarehousingSearch = WarehousingDAO.getAllWarehousing(Warehousing);
            for (Warehousing warehousingSearch: allWarehousingSearch) {
                Commodity commodity = new Commodity();
                commodity.setId(warehousingSearch.getCommodityId());
                Commodity commoditySearch = commodityService.getCommodity(commodity);
                if(null != commoditySearch){
                    //更新入库信息数据
                    if(!commoditySearch.getName().equals(warehousingSearch.getCommodityName())){
                        warehousingSearch.setCommodity(commoditySearch);
                        //更新数据
                        updateWarehousing(warehousingSearch);
                    }
                    warehousingSearch.setCommodity(commoditySearch);

                    allWarehousing.add(warehousingSearch);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return allWarehousing;
    }
    //查询总数
    public Integer getCountWarehousing(Warehousing Warehousing){
        return WarehousingDAO.getCountWarehousing(Warehousing);
    }

    //查询
    public Warehousing getWarehousing(Warehousing Warehousing){
        Warehousing warehousingSearch = null;
        try{
            warehousingSearch = WarehousingDAO.getWarehousing(Warehousing);
            Commodity commodity = new Commodity();
            commodity.setId(warehousingSearch.getCommodityId());
            Commodity commoditySearch = commodityService.getCommodity(commodity);

            warehousingSearch.setCommodity(commoditySearch);

        }catch (Exception e){
            e.printStackTrace();
        }
        return warehousingSearch;
    }

    //增加
    public String addWarehousing(Warehousing Warehousing){
        String isSuccess = "false";
        try{
            //入库时 更新指定商品库存
            Commodity commodity = new Commodity();
            commodity.setId(Warehousing.getCommodityId());
            Commodity commoditySearch = commodityService.getCommodity(commodity);
            //更新库存
            Integer stock = Integer.parseInt(commoditySearch.getStock());
            Integer num = Warehousing.getNum();
            commoditySearch.setStock(String.valueOf(stock + num));

            commodityService.updateCommodity(commoditySearch);

            String time = timeTools.getTime();
            Warehousing.setTime(time);
            Warehousing.setId(timeTools.getTimeInt());

            int resultNum = WarehousingDAO.addWarehousing(Warehousing);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }
    //删除
    public String delWarehousing(Warehousing Warehousing){
        String isSuccess = "false";
        try{
            int resultNum  = WarehousingDAO.delWarehousing(Warehousing);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return isSuccess;
    }
    //修改
    public String updateWarehousing(Warehousing Warehousing){
        String isSuccess = "false";
        try{
            int resultNum = WarehousingDAO.updateWarehousing(Warehousing);
            //
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }
}
